package com.bwhx.eruler.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.CountResult;
import com.bwhx.eruler.bean.Customer;
import com.bwhx.eruler.bean.EChartsResult;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.User;
import com.bwhx.eruler.service.CustomerService;
import com.bwhx.eruler.service.OfflineOrderService;
import com.bwhx.eruler.service.RulerService;
/**
 *
 */
import com.bwhx.eruler.service.UserService;

@Controller
public class DBCountController extends BaseController {
	
	@Autowired
	RulerService rulerService;
	@Autowired
	UserService userService;
	
	@Autowired
	CustomerService customerService;
	
	@Autowired
	OfflineOrderService offlineOrderService; 
	

	
	@ResponseBody
	@RequestMapping(value = "/page.everyone.dbCount.ashx")
	 public List<CountResult> dbCount() {
		ArrayList<CountResult> countResults = new ArrayList<CountResult>();
		
		
		Ruler ruler0 = new Ruler(); //库存
		Ruler ruler1 = new Ruler(); //出库
		Ruler ruler3 = new Ruler();  //已调试待入库
		ruler0.setrState(0);
		ruler1.setrState(1);
		ruler3.setrState(3);
		Integer count0 = rulerService.count(ruler0);
		Integer count1 = rulerService.count(ruler1);
		Integer count3 = rulerService.count(ruler3);
		Integer countUser = userService.count(new User());
		Integer countCustomer = customerService.count(new Customer());
		
		
		countResults.add( new CountResult("未售E尺",count0+count3+""));
		countResults.add( new CountResult("已售E尺",count1+""));
		countResults.add( new CountResult("注册量体师数目",countUser+""));
		countResults.add( new CountResult("上传数据条目",countCustomer+""));
		
	    return countResults;  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.ruler.ashx")
	 public Map<String,Object> ruler() {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		ArrayList<EChartsResult> data = new ArrayList<EChartsResult>();
		String categories[] = {"已调试待入库","在库","出库"};
		resultMap.put("categories", categories);
		
		
		Ruler ruler0 = new Ruler(); //库存
		Ruler ruler1 = new Ruler(); //出库
		Ruler ruler3 = new Ruler();  //已调试待入库
		ruler0.setrState(0);
		ruler1.setrState(1);
		ruler3.setrState(3);
		Integer count0 = rulerService.count(ruler0);
		Integer count1 = rulerService.count(ruler1);
		Integer count3 = rulerService.count(ruler3);
		
		
		
		data.add( new EChartsResult("已调试待入库",count3));
		data.add( new EChartsResult("在库",count0));
		data.add( new EChartsResult("出库",count1));
		//resultMap.put("data", "{value:"+count3+",name:'已调试待入库'},{value:"+count0+",name:'在库'},{value:"+count1+",name:'出库'}");
		resultMap.put("data", data);
		
	    return resultMap;  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.rulermonth.ashx")
	 public List<Integer> rulermonth(String year) {
		List<Integer> resultList = new ArrayList<Integer>(12);
		Integer countValue[] = {0,0,0,0,0,0,0,0,0,0,0,0};
		
		//Date或者String转化为时间戳  
	 /*   SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    String time=year+"-01-01 00:00:00";  
	    Date date = format.parse(time);  
	    Long timeStamp = date.getTime()/1000;*/
		
		
		List<EChartsResult> result = offlineOrderService.countRuler(year);
		if(result.size()==12){
			for (EChartsResult eChartsResult : result) {
					resultList.add(eChartsResult.getValue());
			}
		}else if(result.size()<12){
			for (EChartsResult eChartsResult : result) {
				resultList = Arrays.asList(countValue);
				resultList.set(Integer.parseInt(eChartsResult.getName())-1, eChartsResult.getValue());
			}
		}
		
	    return resultList;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.rulerseller.ashx")
	 public Map<String,Object> rulerseller() {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		ArrayList<String> nameList = new ArrayList<String>();
		ArrayList<Integer> valueList = new ArrayList<Integer>();
		

		List<EChartsResult> result = offlineOrderService.countSeller();
		if(result.size()!=0){
			
			for (EChartsResult eChartsResult : result) {
				nameList.add(eChartsResult.getName());
				valueList.add(eChartsResult.getValue());
			}
			
			resultMap.put("name", nameList);
			resultMap.put("data", valueList);
			
		}else{
			return null;
		}
		
	    return resultMap;  
	  }
}
